package com.ja.openchat.ui.chat.adapter.viewholder;

import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.blankj.utilcode.util.ToastUtils;
import com.google.gson.JsonObject;
import com.ja.openchat.R;
import com.ja.openchat.ui.chat.ConversationFragment;
import com.ja.openchat.ui.chat.annotation.EnableContextMenu;
import com.ja.openchat.ui.chat.annotation.MessageContentType;
import com.ja.openchat.ui.chat.listener.OnRedPacketCallback;
import com.ja.openchat.ui.chat.redpacket.RedPacketDetailActivity;
import com.ja.openchat.views.GetRedPacketDialog;
import com.lxj.xpopup.XPopup;
import com.zy.openchat.core.manager.RedPacketManager;
import com.zy.openchat.core.manager.TelegramManager;
import com.zy.openchat.core.manager.TgResultBlock;
import com.zy.openchat.core.model.message.UiMessage;
import com.zy.openchat.core.model.redpacket.RedPacketInfo;
import com.zy.openchat.core.model.user.UserInfo;

import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;
import me.goldze.mvvmhabit.base.BaseActivity;

@MessageContentType(TelegramManager.MessageType.MessageType_Text_New_Rp)
@EnableContextMenu
public class RedPacketContentViewHolder extends NormalMessageContentViewHolder{
    @BindView(R.id.contentTextView)
    TextView contentTextView;
    @BindView(R.id.redContentView)
    View redContentView;
    @BindView(R.id.contentTextView2)
    TextView contentTextView2;
    @BindView(R.id.iv_red_packet_icon)
    ImageView ivRedPacketIcon;

    public RedPacketContentViewHolder(ConversationFragment fragment, RecyclerView.Adapter adapter, View itemView) {
        super(fragment, adapter, itemView);
    }

    @OnClick(R.id.redContentView)
    public void onClick(View view) {
        if(fragment.getContext() instanceof BaseActivity){
            ((BaseActivity)fragment.getContext()).showProgress();
        }
        if(message.message.rpInfo!=null){
            TelegramManager.getTelegramManager().queryRp(fragment.getContext(),message.message.rpInfo.redPacketId, new TgResultBlock<RedPacketInfo>() {
                @Override
                public void onResult(Map<String, Object> request, JsonObject response, RedPacketInfo obj) {
                    if(fragment.getContext() instanceof BaseActivity){
                        ((BaseActivity)fragment.getContext()).hideProgress();
                    }
                    TelegramManager.RpState rpState = obj.getRpState();
                    message.message.setRedPacketInfo(obj);
                    if((fragment!=null&&!fragment.isGroupChat()&&obj.from == UserInfo.getUserInfo().id)
                            ||rpState == TelegramManager.RpState.RpState_GotADone
                            ||rpState == TelegramManager.RpState.RpState_Got){
                        RedPacketManager.getRedPacketManager().appendId(message.message.rpInfo.redPacketId);
                        RedPacketDetailActivity.startActivity(fragment.getContext(),obj);
                        onBind(message);
                    }else{
                        new XPopup.Builder(fragment.getContext())
                                .asCustom(new GetRedPacketDialog(fragment.getContext(), obj, new OnRedPacketCallback() {
                                    @Override
                                    public void onResult(RedPacketInfo redPacketInfo) {
                                        message.message.setRedPacketInfo(obj);
                                        onBind(message);
                                    }
                                }))
                                .show();
                    }

                }

                @Override
                public void onTimeout(Map<String, Object> request) {
                    if(fragment.getContext() instanceof BaseActivity){
                        ((BaseActivity)fragment.getContext()).hideProgress();
                    }
                    ToastUtils.showShort("打开红包失败");
                }
            });
        }

    }

    @Override
    protected void onBind(UiMessage message) {
        if(message.message.rpInfo!=null){
            contentTextView.setText(message.message.rpInfo.title);
            if(RedPacketManager.getRedPacketManager().isOpendById(message.message.rpInfo.redPacketId)){
                contentTextView2.setVisibility(View.VISIBLE);
                contentTextView2.setText("");
                if(message.message.isOutgoing()){
                    redContentView.setBackgroundResource(R.drawable.shape_red_packet_right_selected);
                }else{
                    redContentView.setBackgroundResource(R.drawable.shape_red_packet_left_selected);
                }
                ivRedPacketIcon.setImageResource(R.mipmap.icon_redpacket_selected);
                if(message.message.getRedPacketInfo()!=null){
                    setRedState(message.message.getRedPacketInfo());
                }else{
                    TelegramManager.getTelegramManager().queryRp(fragment.getContext(),message.message.rpInfo.redPacketId, new TgResultBlock<RedPacketInfo>() {
                        @Override
                        public void onResult(Map<String, Object> request, JsonObject response, RedPacketInfo obj) {
                            message.message.setRedPacketInfo(obj);
                            setRedState(message.message.getRedPacketInfo());
                        }

                        @Override
                        public void onTimeout(Map<String, Object> request) {

                        }
                    });
                }

            }else{
                contentTextView2.setVisibility(View.GONE);
                ivRedPacketIcon.setImageResource(R.mipmap.icon_redpacket_normal);
                if(message.message.isOutgoing()){
                    redContentView.setBackgroundResource(R.drawable.shape_red_packet_right_normal);
                }else{
                    redContentView.setBackgroundResource(R.drawable.shape_red_packet_left_normal);
                }
            }
        }
    }

    private void setRedState(RedPacketInfo obj){
        if(obj==null){
            contentTextView2.setVisibility(View.GONE);
            return;
        }
        TelegramManager.RpState rpState = obj.getRpState();
        switch (rpState){
            case RpState_Expire:
                contentTextView2.setText(R.string.rp_expired_text);
                break;
            case RpState_Done:
                contentTextView2.setText(R.string.rp_finished_text);
                break;
            case RpState_GotADone:
            case RpState_Got:
                contentTextView2.setText(R.string.rp_received_text);
                break;
            case RpState_To_Get:
                contentTextView2.setText(R.string.rp_pending_text);
                break;
        }
    }
}
